package com.example.finaltask;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finaltask.modle.Poster;
import com.example.finaltask.modle.User;
import com.example.finaltask.utils.DBHelper;
import com.example.finaltask.utils.Remove;

import java.util.Date;

public class PostActivity extends AppCompatActivity {
    private SQLiteDatabase db;
    private Poster dynamic;
    private User user;
    private EditText title,content;
    private Button postBtn;
    private String titleText,contentText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        Remove.removeTop(this);
        init();
        //添加一条动态
        postBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkInfo()){
                    insertDynamic();
                    Toast.makeText(getBaseContext(),"上传成功!", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });
    }
    /**
     *@description 初始化
     */
    public void init(){
        //用户信息
        Intent intent = this.getIntent();
        user = (User) intent.getSerializableExtra("user");
        dynamic = (Poster) intent.getSerializableExtra("dynamic");

        title = findViewById(R.id.post_title);
        content = findViewById(R.id.post_content);
        postBtn = findViewById(R.id.post_button);
        postBtn.setVisibility(View.VISIBLE);
    }
    /**
     *@description 插入一条动态
     */
    private void insertDynamic(){
        Poster dynamic = new Poster();
        dynamic.setTitle(titleText);
        dynamic.setContent(contentText);
        //dynamic.setPreviewImageUrl(getImageUrl(contentText));
        dynamic.setCreateTime(new Date());
        dynamic.setUserId(user.getId());

        DBHelper dbhelper = new DBHelper(PostActivity.this,"murmur.db",null,7);
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        //创建存放数据的ContentValues对象
        ContentValues values = new ContentValues();
        values.put("title",dynamic.getTitle());
        values.put("content",dynamic.getContent());
        values.put("previewimageurl",dynamic.getPreviewImageUrl());
        values.put("createtime",String.valueOf(dynamic.getCreateTime()));
        values.put("userid",dynamic.getUserId());
        //数据库执行插入命令
        db.insert("poster", null, values);
    }

    /**
     *@description 最基础的容错判断
     */
    public boolean checkInfo(){
        titleText = title.getText().toString();
        contentText = content.getText().toString();
        if("".equals(titleText)){
            Toast.makeText(getBaseContext(),"标题不能为空", Toast.LENGTH_LONG).show();
        }else if("".equals(contentText)){
            Toast.makeText(getBaseContext(),"内容不能为空", Toast.LENGTH_LONG).show();
        }else {
            return true;
        }
        return false;
    }
}